package com.heakey.common.model;

import com.heakey.common.constant.CommonConstant;
import com.heakey.common.iface.IErrorCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Result<T> extends BaseResult {

    private static final long serialVersionUID = 1L;

    private T data;

    private Result(int code, String msg, T data){
        super(code, msg);
        this.data = data;
    }

    private static <T> Result<T> getInstance(int code, String msg, T data){
        return new Result(code, msg, data);
    }

    public boolean isSuccess(){
        return code == CommonConstant.DEFAULT_SUCCESS_CODE;
    }

    public static Result error(String msg)
    {
        return getInstance(CommonConstant.DEFAULT_ERROR_CODE, msg, null);
    }

    public static Result error(IErrorCode errorCode)
    {
        return getInstance(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static Result error(int code,String msg)
    {
        return getInstance(code, msg, null);
    }

    public static Result success(String msg)
    {
        return getInstance(CommonConstant.DEFAULT_SUCCESS_CODE, msg, null);
    }

    public static <T> Result<T> success(String msg,T data)
    {
        return getInstance(CommonConstant.DEFAULT_SUCCESS_CODE, msg, data);
    }
}